package pers.julio.notepad.PageSwitcher.Bean;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import java.util.Arrays;
import pers.julio.notepad.PageSwitcher.R;


/**
 * ClassName:  SwitchBean
 * Description: <页面跳转控制参数>
 * Author: julio_chan  2020/10/20 12:25
 */

public class SwitchBean implements Parcelable {
    private String mPageName;     //页面名
    private Bundle mBundle;       //切换参数
    private int[] mAnim = null;   //切换动画

    private boolean mAddToBackStack = true;  //是否添加到栈中
    private boolean mNewActivity = false;    //是否起新的Activity

    private int mRequestCode = -1;


    public SwitchBean(String pageName) { mPageName = pageName; }
    public SwitchBean(String pageName, Bundle bundle) {
        mPageName = pageName;
        mBundle = bundle;
    }
    public SwitchBean(String pageName, Bundle bundle, PageAnim pageAnim) {
        mPageName = pageName;
        mBundle = bundle;
        mAnim = convertAnimations(pageAnim);
    }
    public SwitchBean(String pageName, Bundle bundle, int[] anim) {
        mPageName = pageName;
        mBundle = bundle;
        mAnim = anim;
    }
    public SwitchBean(String pageName, Bundle bundle, PageAnim pageAnim, boolean addToBackStack) {
        mPageName = pageName;
        mBundle = bundle;
        mAnim = convertAnimations(pageAnim);
        mAddToBackStack = addToBackStack;
    }
    public SwitchBean(String pageName, Bundle bundle, int[] anim, boolean addToBackStack) {
        mPageName = pageName;
        mBundle = bundle;
        mAnim = anim;
        mAddToBackStack = addToBackStack;
    }
    public SwitchBean(String pageName, Bundle bundle, PageAnim pageAnim, boolean addToBackStack, boolean newActivity) {
        mPageName = pageName;
        mBundle = bundle;
        mAnim = convertAnimations(pageAnim);
        mAddToBackStack = addToBackStack;
        mNewActivity = newActivity;
    }
    public SwitchBean(String pageName, Bundle bundle, int[] anim, boolean addToBackStack, boolean newActivity) {
        mPageName = pageName;
        mBundle = bundle;
        mAnim = anim;
        mAddToBackStack = addToBackStack;
        mNewActivity = newActivity;
    }
    public SwitchBean(String pageName, Bundle bundle, int[] anim, boolean addToBackStack, boolean newActivity, int mRequestCode) {
        mPageName = pageName;
        mBundle = bundle;
        mAnim = anim;
        mAddToBackStack = addToBackStack;
        mNewActivity = newActivity;
        mRequestCode = mRequestCode;
    }


    protected SwitchBean(Parcel in) {
        mPageName = in.readString();
        mBundle = in.readBundle();
        int[] a = {in.readInt(), in.readInt(), in.readInt(), in.readInt()};
        mAnim = a;
        mAddToBackStack = in.readInt() == 1 ? true : false;
        mNewActivity = in.readInt() == 1 ? true : false;
        mRequestCode = in.readInt();
    }

    public String getPageName() { return mPageName; }
    public void setPageName(String pageName) { mPageName = pageName; }

    public boolean isNewActivity() { return mNewActivity; }
    public void setNewActivity(boolean newActivity) { mNewActivity = newActivity; }

    public boolean isAddToBackStack() { return mAddToBackStack; }
    public void setAddToBackStack(boolean addToBackStack) { mAddToBackStack = addToBackStack; }

    public int[] getAnim() { return mAnim; }
    public void setAnim(int[] anim) { mAnim = anim; }

    public Bundle getBundle() { return mBundle; }
    public void setBundle(Bundle bundle) { mBundle = bundle; }

    public int getmRequestCode() { return mRequestCode; }
    public void setmRequestCode(int requestCode) { mRequestCode = requestCode; }

    @Override
    public String toString() {
        return "SwitchBean{" +
                "Name='" + mPageName + '\'' +
                ", Bundle=" + mBundle +
                ", Anim=" + Arrays.toString(mAnim) +
                ", AddToBackStack=" + mAddToBackStack +
                ", NewActivity=" + mNewActivity +
                ", RequestCode=" + mRequestCode +
                '}';
    }


    // 动画转化，根据枚举类返回int数组
    public static int[] convertAnimations(PageAnim pageAnim) {
        if (pageAnim == PageAnim.present) {
            int[] animations = {R.anim.push_in_down, R.anim.push_no_ani, R.anim.push_no_ani, R.anim.push_out_down};
            return animations;
        } else if (pageAnim == PageAnim.fade) {
            int[] animations = {R.anim.alpha_in, R.anim.alpha_out, R.anim.alpha_in, R.anim.alpha_out};
            return animations;
        } else if (pageAnim == PageAnim.slide) {
            int[] animations = {R.anim.slide_in_right, R.anim.slide_out_left, R.anim.slide_in_left, R.anim.slide_out_right};
            return animations;
        }else if (pageAnim == PageAnim.zoom) {
            int[] animations = {R.anim.zoom_in, R.anim.zoom_out, R.anim.zoom_in, R.anim.zoom_out};
            return animations;
        }
        return null;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel out, int flags) {
        if (mPageName == null) {
            mPageName = "";
        }
        if (mBundle == null) {
            mBundle = new Bundle();
        }
        if (mAnim == null) {
            int[] a = {-1, -1, -1, -1};
            mAnim = a;
        }
        out.writeString(mPageName);
        mBundle.writeToParcel(out, flags);
        if (mAnim != null && mAnim.length == 4) {
            out.writeInt(mAnim[0]);
            out.writeInt(mAnim[1]);
            out.writeInt(mAnim[2]);
            out.writeInt(mAnim[3]);
        } else {
            out.writeInt(-1);
            out.writeInt(-1);
            out.writeInt(-1);
            out.writeInt(-1);
        }
        out.writeInt(mAddToBackStack ? 1 : 0);
        out.writeInt(mNewActivity ? 1 : 0);
        out.writeInt(mRequestCode);
    }
    public static final Parcelable.Creator<SwitchBean> CREATOR = new Parcelable.Creator<SwitchBean>() {
        @Override
        public SwitchBean createFromParcel(Parcel in) {
            return new SwitchBean(in);
        }
        @Override
        public SwitchBean[] newArray(int size) {
            return new SwitchBean[size];
        }
    };

}
